package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.service.impl.BaseService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.FriendRequest;
import com.ruoyi.system.domain.vo.FriendRelationshipVO;
import com.ruoyi.system.domain.vo.FriendRequestVO;
import com.ruoyi.system.mapper.FriendRequestMapper;
import com.ruoyi.system.service.IFriendRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FriendRelationshipMapper;
import com.ruoyi.system.domain.FriendRelationship;
import com.ruoyi.system.service.IFriendRelationshipService;

/**
 * 好友关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Service("friendRelationshipService")
public class FriendRelationshipServiceImpl extends BaseService<FriendRelationshipMapper, FriendRelationship, FriendRelationshipVO> implements IFriendRelationshipService {
    @Autowired
    private FriendRelationshipMapper friendRelationshipMapper;

    /**
     * 查询好友关系
     * 
     * @param relationshipId 好友关系主键
     * @return 好友关系
     */
    @Override
    public FriendRelationship selectFriendRelationshipByRelationshipId(Long relationshipId)
    {
        return friendRelationshipMapper.selectFriendRelationshipByRelationshipId(relationshipId);
    }

    /**
     * 查询好友关系列表
     * 
     * @param friendRelationship 好友关系
     * @return 好友关系
     */
    @Override
    public List<FriendRelationship> selectFriendRelationshipList(FriendRelationship friendRelationship)
    {
        return friendRelationshipMapper.selectFriendRelationshipList(friendRelationship);
    }

    /**
     * 新增好友关系
     * 
     * @param friendRelationship 好友关系
     * @return 结果
     */
    @Override
    public int insertFriendRelationship(FriendRelationship friendRelationship)
    {
        friendRelationship.setCreateTime(DateUtils.getNowDate());
        return friendRelationshipMapper.insertFriendRelationship(friendRelationship);
    }

    /**
     * 修改好友关系
     * 
     * @param friendRelationship 好友关系
     * @return 结果
     */
    @Override
    public int updateFriendRelationship(FriendRelationship friendRelationship)
    {
        friendRelationship.setUpdateTime(DateUtils.getNowDate());
        return friendRelationshipMapper.updateFriendRelationship(friendRelationship);
    }

    /**
     * 批量删除好友关系
     * 
     * @param relationshipIds 需要删除的好友关系主键
     * @return 结果
     */
    @Override
    public int deleteFriendRelationshipByRelationshipIds(Long[] relationshipIds)
    {
        return friendRelationshipMapper.deleteFriendRelationshipByRelationshipIds(relationshipIds);
    }

    /**
     * 删除好友关系信息
     * 
     * @param relationshipId 好友关系主键
     * @return 结果
     */
    @Override
    public int deleteFriendRelationshipByRelationshipId(Long relationshipId)
    {
        return friendRelationshipMapper.deleteFriendRelationshipByRelationshipId(relationshipId);
    }

    @Override
    public boolean handleAgree(FriendRequestVO infoVO) {

        FriendRelationship senderVO = new FriendRelationship();
        senderVO.setUserId(infoVO.getSenderId());
        senderVO.setFriendUserId(infoVO.getReceiverId());
        senderVO.setIsBlacklisted(0L);
        setVOInfo(senderVO);

        int i = friendRelationshipMapper.insertFriendRelationship(senderVO);

        FriendRelationship receiverVO = new FriendRelationship();
        receiverVO.setUserId(infoVO.getReceiverId());
        receiverVO.setFriendUserId(infoVO.getSenderId());
        receiverVO.setIsBlacklisted(0L);
        setVOInfo(receiverVO);

        i += friendRelationshipMapper.insertFriendRelationship(receiverVO);
        return 2 == i;
    }
}
